//
//  CJCommonTool.m
//  CJMobileAdapter
//
//  Created by ly on 2024/3/1.
//

#import "CJCommonTool.h"

@implementation CJCommonTool

// 获取当前根控制器
+ (UIViewController *)currentRootController {
    UIViewController *rootController = [self getKeyWindow].rootViewController;
    return rootController;
}

// 获取当前window
+ (UIWindow *)getKeyWindow {
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
            if (windowScene.activationState == UISceneActivationStateForegroundActive)
            {
                for (UIWindow *window in windowScene.windows)
                {
                    if (window.isKeyWindow)
                    {
                        return window;
                    }
                }
            }
        }
    } else {
        // 添加到当前window上，并置顶到最上层
        UIWindow * window = [UIApplication sharedApplication].delegate.window;
        if (window) {
            return window;
        }
        return [UIApplication sharedApplication].keyWindow;
    }
    return nil;
}

@end
